﻿using Aker.DevTeam;
using Aker.Uretim;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using Extensions;
using System;
using System.Data;


namespace Aker.Product
{
    public partial class YeniImalatKarti4 : XForm
    {

        DataTable dtHammadde = null;
        DataTable dtRecete = null;
        DataTable dtMaliyet = null;

        int BazProductId;
        string bazArtikel;
        int UretimTipi = 0;
        int POrderLinesId;
        int StokKartiTipi = 1;


        public delegate void ProductInsertedEventHandler(int ProductId, string Artikel, int StokKartiTipi);
        public event ProductInsertedEventHandler ProductInserted;
        public YeniImalatKarti4()
        {
            InitializeComponent();
        }

        public YeniImalatKarti4(int StokKartiTipi)
        {
            InitializeComponent();
            this.StokKartiTipi = StokKartiTipi;
          
        }

        public YeniImalatKarti4(string bazArtikel, int bazProductId, int uretimTipi, decimal qty, int pOrderLinesId)
        {
            InitializeComponent();

            teQty.EditValue = qty;
            teQty.Visible = false;

            this.BazProductId = bazProductId;
            this.bazArtikel = bazArtikel;

            this.UretimTipi = uretimTipi;
            this.POrderLinesId = pOrderLinesId;

            string[] g20 = new string[] { "20.", "10.", "5000", "7000", "8000", "9000", "9500" };
            string[] g11 = new string[] { "11.", "12.", "30." };
            bool found = false;
             
        }

        private void StokKarti_Load(object sender, EventArgs e)
        { 
            if (Data.DB.Currency.Rows.Count == 0)
                Data.TAM.CurrencyTableAdapter.Fill(Data.DB.Currency);


            switch (StokKartiTipi)
            {
                case 1:
                    Load20();
                    break;
                case 2:
                    //Load11();
                    break;
                case 3:
                    break;
                case 4:
                    break;
            }
            //       int x = gv1.Container.Components.Count;
        }

        #region 20
        private void Load20()
        {
            //gvMaliyet20.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
            //dtHammadde = Data.DM.ExecuteDataSet("GetHammaddeStok").Tables[2];

            //dtRecete = new DataTable();
            //dtRecete.Columns.Add("Oran", typeof(int));
            //dtRecete.Columns.Add("HammaddeId", typeof(int));
            //dtRecete.Columns.Add("ElyafId", typeof(int));
            //dtRecete.Columns.Add("IplikId", typeof(int));
            //dtRecete.Columns.Add("HarmanKg", typeof(decimal));
            //dtRecete.Columns.Add("KG", typeof(double));
            //dtRecete.Columns.Add("Akfil", typeof(double));
            //dtRecete.Columns.Add("Bilecik", typeof(double));

            //DataTable dtIp = dtHammadde.Select("HammaddeId < 1000000").CopyToDataTable();
            //gleZeminIpi.Properties.Assign(Utility.GetAutoCompGLE(dtIp, "Hammadde", "HammaddeId"));
            //gleZeminIpi.EditValue = 1;

            //if (Data.DB.Product.Rows.Count == 0)
            //    Data.TAM.ProductTableAdapter.Fill(Data.DB.Product);
            //DataTable dtProduct = Data.DB.Product.DefaultView.ToTable(true, "ProductId", "Article", "Weight", "ToleranceWeight", "Width", "ToleranceWidth", "Composition", "ToleranceComposition", "Design", "PartiNo");


            //gleBazArtikel20.Properties.Assign(Utility.GetAutoCompGLE(dtProduct, "Article", "ProductId"));
            //if (bazArtikel != null)
            //{
            //    gleBazArtikel20.EditValue = dtProduct.Select("Article = '" + bazArtikel + "'")[0]["ProductId"];
            //    teQty.Visible = false;
            //}
            //else
            //    teQty.Visible = true;

            //cmbUretimTipi20.SelectedIndex = UretimTipi;
            //UretimTipiChanged20();

            //cmbGrup20.SelectedIndex = 0;
            //cmbKompozisyon20.SelectedIndex = 0;
            //cmbCins.SelectedIndex = 0;

            //if (BazProductId > 0)
            //    LoadProductDetail20();
            //else
            //{
            //    InitReceteGv(null, gv1, gv2, gv3, gv4);
            //    InitMaliyetGv(null);
            //}

            //gv1.CellValueChanged += gvKafa_CellValueChanged;
            //gv2.CellValueChanged += gvKafa_CellValueChanged;
            //gv3.CellValueChanged += gvKafa_CellValueChanged;
            //gv4.CellValueChanged += gvKafa_CellValueChanged;

            //cmbUretimTipi20.SelectedIndexChanged += cmbUretimTipi_SelectedIndexChanged;
        }

        private void InitMaliyetGv(DataTable dtMaliyet)
        {
            //if (dtMaliyet == null)
            //{
            //    dtMaliyet = new DataTable();
            //    dtMaliyet.Columns.Add("MaliyetAciklama", typeof(string));
            //}

            //gvMaliyet20.Init(dtMaliyet);
            //gvMaliyet20.Columns["CurrencyId"].ColumnEdit = Utility.GetAutoCompGLE(Data.DB.Currency, "Name", "CurrencyId");
        }

        private void InitReceteGv(DataTable dtData = null, params GridView[] gvs)
        {
            //DataTable dt = dtRecete.Clone();
            //if (dtData != null)
            //{
            //    foreach (DataRow dr in dtData.Rows)
            //    {
            //        DataRow drHam = dtHammadde.Select("HammaddeId=" + dr["HammaddeId"])[0];
            //        dt.Rows.Add(dr["Oran"],
            //                    dr["HammaddeId"],
            //                    dr["ElyafId"],
            //                    dr["IplikId"],
            //                    dr["HarmanKg"],
            //                    drHam["KG"],
            //                    drHam["Akfil"],
            //                    drHam["Bilecik"]
            //                    );
            //    }
            //}

            //foreach (GridView gv in gvs)
            //{
            //    gv.Init(dt.Copy());
            //    gv.OptionsView.ShowViewCaption = false;
            //    gv.MakeReadOnly("HammaddeId", "Oran", "HarmanKg");
            //    gv.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            //    gv.Columns["HammaddeId"].ColumnEdit = Utility.GetAutoCompGLE(dtHammadde, "Hammadde", "HammaddeId");
            //    gv.SetCaptions("HammaddeId:Hammadde");
            //    gv.MakeColumnInvisible("ElyafId", "IplikId");
            //    gv.FormatAsNumber("HarmanKg");
            //    gv.BestFitColumns();
            //}
        }

        private void UretimTipiChanged20()
        {
            //if (cmbUretimTipi20.SelectedIndex < 2)
            //{
            //    cmbGrup20.ReadOnly = false;
            //    cmbKompozisyon20.ReadOnly = false;
            //    txtDesenNo.ReadOnly = false;
            //    seGirisGramaj20.ReadOnly = false;
            //    seFinalGramaj20.ReadOnly = false;
            //}
            //else
            //{
            //    LoadProductDetail20();
            //    cmbGrup20.ReadOnly = true;
            //    cmbKompozisyon20.ReadOnly = true;
            //    txtDesenNo.ReadOnly = true;
            //    seGirisGramaj20.ReadOnly = true;
            //    seFinalGramaj20.ReadOnly = true;
            //}

            GetNewCode();
        }

        private void LoadProductDetail20()
        {
            //LoadSentezDetail();

            //DataSet dsProductDetail = Data.DM.ExecuteDataSet("GetProductDetail", BazProductId);

            //DataTable dtMaliyet = dsProductDetail.Tables[1];
            //InitMaliyetGv(dtMaliyet);


            //if (dsProductDetail.Tables[0].Rows.Count == 0)
            //{
            //    InitReceteGv(null, gv1, gv2, gv3, gv4);
            //    return;
            //}

            //DataRow drDetail = dsProductDetail.Tables[0].Rows[0];

            //cmbGrup20.SelectedIndex = drDetail["ProductGrupId"].ToComboIndex();
            //cmbKompozisyon20.SelectedIndex = drDetail["KompozisyonId"].ToComboIndex();
            //txtDesenNo.Text = drDetail["DesenNo"].ToStringNN();
            //txtRenk.Text = drDetail["Renk"].ToStringNN();
            //cmbCins.SelectedIndex = drDetail["ProductCinsId"].ToComboIndex();
            //seFire.EditValue = drDetail["Fire"].ToIntNN(0);
            //seGirisGramaj20.EditValue = drDetail["GirisGr"].ToIntNN(0);
            //seFinalGramaj20.EditValue = drDetail["FinishGr"].ToIntNN(0);

            //txtKafa1.Text = drDetail["KafaRenk1"].ToStringNN();
            //txtKafa2.Text = drDetail["KafaRenk2"].ToStringNN();
            //txtKafa3.Text = drDetail["KafaRenk3"].ToStringNN();
            //txtKafa4.Text = drDetail["KafaRenk4"].ToStringNN();

            //seKafa1.EditValue = drDetail["KafaOran1"].ToIntNN(0);
            //seKafa2.EditValue = drDetail["KafaOran2"].ToIntNN(0);
            //seKafa3.EditValue = drDetail["KafaOran3"].ToIntNN(0);
            //seKafa4.EditValue = drDetail["KafaOran4"].ToIntNN(0);

            //cmbCins1.SelectedIndex = drDetail["KafaCins1"].ToComboIndex();
            //cmbCins2.SelectedIndex = drDetail["KafaCins2"].ToComboIndex();
            //cmbCins3.SelectedIndex = drDetail["KafaCins3"].ToComboIndex();
            //cmbCins4.SelectedIndex = drDetail["KafaCins4"].ToComboIndex();

            //if (teQty.Visible == true)
            //    teQty.Text = drDetail["QtyManuel"].ToStringNN();

            //DataRow[] drs = dsProductDetail.Tables[1].Select("KafaNo=" + 1);
            //if (drs.Length > 0)
            //    InitReceteGv(drs.CopyToDataTable(), gv1);
            //else
            //    InitReceteGv(null, gv1);

            //drs = dsProductDetail.Tables[1].Select("KafaNo=" + 2);
            //if (drs.Length > 0)
            //    InitReceteGv(drs.CopyToDataTable(), gv2);
            //else
            //    InitReceteGv(null, gv2);

            //drs = dsProductDetail.Tables[1].Select("KafaNo=" + 3);
            //if (drs.Length > 0)
            //    InitReceteGv(drs.CopyToDataTable(), gv3);
            //else
            //    InitReceteGv(null, gv3);

            //drs = dsProductDetail.Tables[1].Select("KafaNo=" + 4);
            //if (drs.Length > 0)
            //    InitReceteGv(drs.CopyToDataTable(), gv4);
            //else
            //    InitReceteGv(null, gv4);

            //gleZeminIpi.EditValue = drDetail["ZeminIpiId"].ToIntNN();

            //txtSIKLIK20.Text = drDetail["SIKLIK"].ToStringNN();
            //seTumblerSuresi.EditValue = drDetail["TumblrSuresi"].ToIntNN(0);
            //seRamIsi.EditValue = drDetail["RamIsi"].ToIntNN(0);
            //seFinalEn.EditValue = drDetail["FinalEn"].ToIntNN(0);

            //seOrguTuru.EditValue = drDetail["OrguTuru"].ToIntNN(0);
            //txtHavalar.Text = drDetail["Havalar"].ToStringNN();
            //txtTumblerIsisi.Text = drDetail["TumblrIsisi"].ToStringNN();
            //txtHavBoyu.Text = drDetail["HavBoyu"].ToStringNN();
            //cmbMakasGrubu.Text = drDetail["MakasGrubu"].ToStringNN();
            //txtAciklama.Text = drDetail["Aciklama"].ToStringNN();

            //cmbTarakGrubu.SelectedIndex = drDetail["TarakGrubuId"].ToComboIndex();
            //cmbZeminIpiTipi.SelectedIndex = drDetail["ZeminIpiTipiId"].ToComboIndex();
            //cmbIgneCesidi.SelectedIndex = drDetail["IgneCesidiId"].ToComboIndex();
            //cmbRamId.SelectedIndex = drDetail["RamId"].ToComboIndex();
            //cmbDekatur.SelectedIndex = drDetail["DekaturId"].ToComboIndex();

            //chkDink.Checked = (bool)drDetail["Dink"];

            //if (drDetail["Sardon"] != DBNull.Value)
            //    cmbSardon.SelectedIndex = drDetail["Sardon"].ToIntNN();
        }

        private void Save20(AkerDB.ProductDetayRow drDetay)
        {
            //drDetay.ProductGrupId = cmbGrup20.SelectedIndex + 1;
            //drDetay.Renk = txtRenk.Text;
            //drDetay.DesenNo = txtDesenNo.Text;
            //drDetay.KompozisyonId = cmbKompozisyon20.SelectedIndex + 1;
            //drDetay.ProductCinsId = cmbCins.SelectedIndex + 1;

            ////TODO: bu alan kaldırılacak drDetay.ReferansArtikel = gleReferans20.EditValue.ToStringNN();
            //drDetay.KafaRenk1 = txtKafa1.Text;
            //drDetay.KafaOran1 = seKafa1.EditValue.ToIntNN();
            //drDetay.KafaCins1 = cmbCins1.SelectedIndex + 1;

            //drDetay.FinishGr = seFinalGramaj20.EditValue.ToIntNN();
            //if (drDetay.FinishGr < 1)
            //    drDetay.SetFinishGrNull();

            //drDetay.GirisGr = seGirisGramaj20.EditValue.ToIntNN(); ;
            //if (drDetay.GirisGr < 1)
            //    drDetay.SetGirisGrNull();

            //drDetay.Fire = seFire.EditValue.ToIntNN();

            //foreach (DataRow dr in (gv1.DataSource as DataView).Table.Rows)
            //{
            //    AkerDB.ProductHammaddeRow drHammadde = Data.DB.ProductHammadde.NewProductHammaddeRow();
            //    drHammadde.ProductId = drDetay.ProductId;
            //    drHammadde.KafaNo = 1;
            //    if (dr["ElyafId"].ToIntNN() > 0)
            //        drHammadde.ElyafId = dr["ElyafId"].ToIntNN();
            //    else
            //        drHammadde.IplikId = dr["IplikId"].ToIntNN();
            //    drHammadde.Oran = dr["Oran"].ToIntNN();
            //    drHammadde.HarmanKg = dr["HarmanKg"].ToDecimalNN();

            //    Data.DB.ProductHammadde.Rows.Add(drHammadde);
            //    Data.TAM.ProductHammaddeTableAdapter.Update(drHammadde);
            //}


            //if (seKafa2.EditValue.ToIntNN() > 0)
            //{
            //    drDetay.KafaOran2 = seKafa2.EditValue.ToIntNN();
            //    drDetay.KafaRenk2 = txtKafa2.Text;
            //    drDetay.KafaCins2 = cmbCins2.SelectedIndex + 1;

            //    foreach (DataRow dr in (gv2.DataSource as DataView).Table.Rows)
            //    {
            //        AkerDB.ProductHammaddeRow drRecete = Data.DB.ProductHammadde.NewProductHammaddeRow();
            //        drRecete.ProductId = drDetay.ProductId;
            //        drRecete.KafaNo = 2;
            //        if (dr["ElyafId"].ToIntNN() > 0)
            //            drRecete.ElyafId = dr["ElyafId"].ToIntNN();
            //        else
            //            drRecete.IplikId = dr["IplikId"].ToIntNN();
            //        drRecete.Oran = dr["Oran"].ToIntNN();
            //        drRecete.HarmanKg = dr["HarmanKg"].ToIntNN();
            //        Data.DB.ProductHammadde.Rows.Add(drRecete);
            //        Data.TAM.ProductHammaddeTableAdapter.Update(drRecete);
            //    }
            //}

            //if (seKafa3.EditValue.ToIntNN() > 0)
            //{
            //    drDetay.KafaOran3 = seKafa3.EditValue.ToIntNN();
            //    drDetay.KafaRenk3 = txtKafa3.Text;
            //    drDetay.KafaCins3 = cmbCins3.SelectedIndex + 1;

            //    foreach (DataRow dr in (gv3.DataSource as DataView).Table.Rows)
            //    {
            //        AkerDB.ProductHammaddeRow drRecete = Data.DB.ProductHammadde.NewProductHammaddeRow();
            //        drRecete.ProductId = drDetay.ProductId;
            //        drRecete.KafaNo = 3;
            //        if (dr["ElyafId"].ToIntNN() > 0)
            //            drRecete.ElyafId = dr["ElyafId"].ToIntNN();
            //        else
            //            drRecete.IplikId = dr["IplikId"].ToIntNN();
            //        drRecete.Oran = dr["Oran"].ToIntNN();
            //        drRecete.HarmanKg = dr["HarmanKg"].ToDecimalNN();
            //        Data.DB.ProductHammadde.Rows.Add(drRecete);
            //        Data.TAM.ProductHammaddeTableAdapter.Update(drRecete);
            //    }
            //}

            //if (seKafa4.EditValue.ToIntNN() > 0)
            //{
            //    drDetay.KafaOran4 = seKafa4.EditValue.ToIntNN();
            //    drDetay.KafaRenk4 = txtKafa4.Text;
            //    drDetay.KafaCins4 = cmbCins4.SelectedIndex + 1;

            //    foreach (DataRow dr in (gv4.DataSource as DataView).Table.Rows)
            //    {
            //        AkerDB.ProductHammaddeRow drRecete = Data.DB.ProductHammadde.NewProductHammaddeRow();
            //        drRecete.ProductId = drDetay.ProductId;
            //        drRecete.KafaNo = 4;
            //        if (dr["ElyafId"].ToIntNN() > 0)
            //            drRecete.ElyafId = dr["ElyafId"].ToIntNN();
            //        else
            //            drRecete.IplikId = dr["IplikId"].ToIntNN();
            //        drRecete.Oran = dr["Oran"].ToIntNN();
            //        drRecete.HarmanKg = dr["HarmanKg"].ToDecimalNN();
            //        Data.DB.ProductHammadde.Rows.Add(drRecete);
            //        Data.TAM.ProductHammaddeTableAdapter.Update(drRecete);
            //    }
            //}


            //drDetay.TarakGrubuId = cmbTarakGrubu.SelectedIndex + 1;

            //drDetay.ZeminIpiTipiId = cmbZeminIpiTipi.SelectedIndex + 1;
            //drDetay.ZeminIpiId = gleZeminIpi.EditValue.ToIntNN();

            //drDetay.SIKLIK = txtSIKLIK20.Text;
            //if (drDetay.SIKLIK == "")
            //    drDetay.SetSIKLIKNull();

            //drDetay.OrguTuru = seOrguTuru.EditValue.ToIntNN();
            //if (drDetay.OrguTuru < 1)
            //    drDetay.SetOrguTuruNull();

            //drDetay.IgneCesidiId = cmbIgneCesidi.SelectedIndex + 1;

            //drDetay.Aciklama = txtAciklama.Text;
            //if (drDetay.Aciklama == "")
            //    drDetay.SetAciklamaNull();

            //drDetay.Havalar = txtHavalar.Text;
            //if (drDetay.Havalar == "")
            //    drDetay.SetHavalarNull();

            //drDetay.MakasGrubu = cmbMakasGrubu.EditValue.ToStringNN();
            //if (drDetay.MakasGrubu == "")
            //    drDetay.SetMakasGrubuNull();

            //drDetay.TumblrSuresi = seTumblerSuresi.EditValue.ToIntNN();
            //if (drDetay.TumblrSuresi < 0)
            //    drDetay.SetTumblrSuresiNull();

            //drDetay.TumblrIsisi = txtTumblerIsisi.Text;
            //if (drDetay.TumblrIsisi == "")
            //    drDetay.SetTumblrIsisiNull();

            //drDetay.HavBoyu = txtHavBoyu.Text;
            //if (drDetay.HavBoyu == "")
            //    drDetay.SetHavBoyuNull();

            //drDetay.DekaturId = cmbDekatur.SelectedIndex + 1;
            //drDetay.Dink = chkDink.Checked;

            //drDetay.Sardon = cmbSardon.SelectedIndex + 1;
            //drDetay.RamId = cmbRamId.SelectedIndex + 1;


            //drDetay.RamIsi = seRamIsi.EditValue.ToIntNN();
            //if (drDetay.RamIsi < 0)
            //    drDetay.SetRamIsiNull();

            //drDetay.FinalEn = seFinalEn.EditValue.ToIntNN();
            //if (drDetay.FinalEn < 1)
            //    drDetay.SetFinalEnNull();
            //if (teQty.Visible == true)
            //    drDetay.QtyManuel = teQty.EditValue.ToDecimalNN();
        }

        public void HarmanHesapla()
        {
            //double girisGramaj = Convert.ToDouble(seGirisGramaj20.EditValue);
            //if (girisGramaj < 1)
            //    return;

            //int qty = teQty.EditValue.ToIntNN();
            //if (qty < 1)
            //    return;

            //int ipAdedi = cmbZeminIpiTipi.SelectedIndex + 1;
            //string zeminIpi = gleZeminIpi.Text.Trim();

            //int zeminIpiGramaji = zeminIpi.Split('/')[0].ToIntNN() / 3;

            //double fire = 1 + seFire.EditValue.ToIntNN() / 100.0;

            //double zeminIpiOrani = (zeminIpiGramaji / girisGramaj) * ipAdedi;
            //double carpan = (teQty.EditValue.ToIntNN() * girisGramaj / 1000.0) * 1.5;

            //double zeminIpiQty = carpan * zeminIpiOrani;
            //double hammaddeQty = carpan * (1 - zeminIpiOrani) * fire;

            //HarmanHesapla(seKafa1, gv1, hammaddeQty);
            //HarmanHesapla(seKafa2, gv2, hammaddeQty);
            //HarmanHesapla(seKafa3, gv3, hammaddeQty);
            //HarmanHesapla(seKafa4, gv4, hammaddeQty);
        }

        private void HarmanHesapla(SpinEdit seKafa, GridView gvKafa, double hammaddeQty)
        {
            //if (gvKafa.DataSource == null)
            //    return;

            //gvKafa.EndEditing();
            //double kafaOran = seKafa.EditValue.ToIntNN() / 100.0;
            //DataTable dtKafa = (gvKafa.DataSource as DataView).Table;
            //foreach (DataRow dr in dtKafa.Rows)
            //{
            //    double satirOran = dr["Oran"].ToIntNN() / 100.0;
            //    double qty = hammaddeQty * kafaOran * satirOran;

            //    dr["HarmanKg"] = qty;
            //}
        }



        private void seHarman_EditValueChanged(object sender, EventArgs e)
        {
            HarmanHesapla();
        }

        private void gvHarman_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            HarmanHesapla();
        }

        private void gvKafa_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName != "HammaddeId")
                return;

            DevExpress.XtraGrid.Views.Grid.GridView gv = sender as DevExpress.XtraGrid.Views.Grid.GridView;

            DataRow dr = gv.GetFocusedDataRow();
            int HammaddeId = e.Value.ToIntNN();

            DataRow drHammadde = dtHammadde.Select("HammaddeId=" + HammaddeId)[0];
            dr["KG"] = drHammadde["KG"];
            dr["Akfil"] = drHammadde["Akfil"];
            dr["Bilecik"] = drHammadde["Bilecik"];

            if (HammaddeId > 1000000)
                dr["ElyafId"] = HammaddeId - 1000000;
            else
                dr["IplikId"] = HammaddeId;

            gv.BestFitColumns();
        }

        private void txtDesenNo_EditValueChanged(object sender, EventArgs e)
        {
            //if (txtDesenNo.Text.ToIntNN() > 1)
            //    txtRenk.Text = txtDesenNo.Text.ToIntNN() + " Renkli";
        }

        #endregion

     


        #region Ortak

        private void GetNewCode()
        {
            //if (bazArtikel != null)
            //{
            //    string filter = "";
            //    string result = "";

            //    int tip = 0;

            //    //20 , 10 , 30 : yıllı devam edenler , aynı form

            //    //0 , 4 , 5, 6 , 7 , 8 , 9 : 20 li formunun kullan. kodda sadece sıradaki noyu ver.


            //    if (
            //            bazArtikel.StartsWith("10") ||
            //            bazArtikel.StartsWith("11") ||
            //            bazArtikel.StartsWith("20")
            //        )
            //    {
            //        tip = 1;
            //        filter = bazArtikel.Substring(0, 3) + DateTime.Now.Year.ToString().Substring(2, 2) + "%";

            //    }
            //    else if (bazArtikel.StartsWith("30"))
            //    {
            //        tip = 2;
            //        filter = "30.%";
            //    }
            //    else if (bazArtikel.StartsWith("DT"))
            //    {
            //        tip = 3;
            //        filter = "DT.%";
            //    }
            //    else if (bazArtikel.StartsWith("DP"))
            //    {
            //        tip = 4;
            //        filter = "DP.%";
            //    }
            //    else if (bazArtikel.StartsWith("F."))
            //    {
            //        tip = 5;
            //        filter = "F.%";
            //    }
            //    else if (bazArtikel.StartsWith("4") || bazArtikel.StartsWith("5") || bazArtikel.StartsWith("6"))
            //    {
            //        tip = 6;
            //        filter = "4%";
            //    }
            //    else if (bazArtikel.StartsWith("7"))
            //    {
            //        tip = 7;
            //        filter = "7%";
            //    }
            //    else if (bazArtikel.StartsWith("8"))
            //    {
            //        tip = 8;
            //        filter = "8%";
            //    }
            //    else if (bazArtikel.StartsWith("95"))
            //    {
            //        tip = 9;
            //        filter = "95%";
            //    }
            //    else if (bazArtikel.StartsWith("9"))
            //    {
            //        tip = 10;
            //        filter = "9%";
            //    }


            //    string code = Data.DM.ExecuteScalar("GetLastCode", filter).ToStringNN();

            //    //lblYeniKod.Text = code;

            //    int no = 0;
            //    if (tip == 1)
            //    {
            //        if (code != "")
            //            no = code.Split('-')[0].Substring(5).ToIntNN();
            //        else
            //        {
            //            no = 0;
            //            code = bazArtikel.Substring(0, 2) + "." + DateTime.Now.Year.ToString().Substring(2, 2);
            //        }

            //        if (cmbUretimTipi20.SelectedIndex == 0) //aynı artikel
            //        {
            //            string lastNo = "";

            //            char c1 = bazArtikel.Substring(bazArtikel.Length - 1, 1).ToCharArray()[0];
            //            if (Char.IsDigit(c1))
            //            {
            //                char c2 = bazArtikel.Substring(bazArtikel.Length - 2, 1).ToCharArray()[0];
            //                if (Char.IsDigit(c2))
            //                    lastNo = ((c2.ToString() + c1.ToString()).ToIntNN() + 1).ToStringNN();
            //                else
            //                    lastNo = (c1.ToString().ToIntNN() + 1).ToStringNN();
            //            }
            //            else
            //                lastNo = "1";

            //            result = bazArtikel.Substring(0, 10) + lastNo;
            //        }

            //        else if (cmbUretimTipi20.SelectedIndex == 1)
            //            result = code.Substring(0, 5) + (no + 1).ToString().PadLeft(3, '0') + "-A";
            //        else if (cmbUretimTipi20.SelectedIndex == 2)
            //            result = bazArtikel.Split('-')[0] + "-" + (char)(bazArtikel.Split('-')[1].ToCharArray()[0] + 1);

            //    }
            //    else if (tip == 2)
            //    {
            //        no = code.Split('-')[0].Split('.')[1].ToIntNN();
            //        result = code.Substring(0, 3) + (no + 1).ToString().PadLeft(5, '0') + "-A";
            //    }
            //    else if (tip == 3)
            //    {
            //        no = code.Split('-')[0].Split('.')[1].ToIntNN();
            //        result = code.Substring(0, 3) + (no + 1).ToString().PadLeft(4, '0') + "-A";
            //    }
            //    else if (tip == 4)
            //    {
            //        no = code.Split('-')[0].Split('.')[1].ToIntNN();
            //        result = code.Substring(0, 3) + (no + 1).ToString().PadLeft(3, '0') + "-A";
            //    }
            //    else if (tip == 5)
            //    {
            //        no = code.Split('-')[0].Split('.')[1].ToIntNN();
            //        result = code.Substring(0, 2) + (no + 1).ToString().PadLeft(4, '0') + "-A";
            //    }
            //    else if (tip == 6 || tip == 7 || tip == 8 || tip == 9 || tip == 10)
            //    {
            //        no = code.Split('-')[0].ToIntNN();
            //        result = (no + 1).ToString().PadLeft(4, '0') + "-A";
            //    }

            //    switch (StokKartiTipi)
            //    {
            //        case 1:
            //            lblYeniKod20.Text = result;
            //            break;
            //        case 2:
            //            lblYeniKod11.Text = result;
            //            break;
            //        case 3:
            //            lblYeniKodMOI.Text = result;
            //            break;
            //        case 4:
            //            lblYeniKodDPDT.Text = result; ;
            //            break;
            //    }
            //}
        }


        private void LoadSentezDetail()
        {
            DataSet ds = null;
            try
            {
                ds = Data.DMServer.ExecuteDataSet("GetSentezArtikel", bazArtikel);
            }
            catch (Exception ex) //bağlantı yoksa!
            {
                //XtraMessageBox.Show(ex.ToString());
                ds = Data.DMLocal.ExecuteDataSet("GetSentezArtikelLocal", bazArtikel);
            }


            gcSentez.Visible = (ds.Tables[0].Rows.Count > 0);

            if (!gcSentez.Visible)
                return;

            gcSentez.Text = bazArtikel + " Sentez Detayı";

            DataRow drSK = ds.Tables[0].Rows[0];
            DataTable dt = new DataTable();
            dt.Columns.Add(Language.Sentez.Attribute, typeof(string));
            dt.Columns.Add(Language.Sentez.Value, typeof(string));

            foreach (DataColumn dc in ds.Tables[0].Columns)
                dt.Rows.Add(dc.ColumnName, drSK[dc].ToStringNN());

            gvSK.Init(dt);
            gvSK.ViewCaption = Language.Sentez.StokKarti;
            gvSK.BestFitColumns();

            gvHammadde.Init(ds.Tables[1]);
            gvHammadde.ViewCaption = Language.Sentez.Hammadde;
            gvHammadde.MakeColumnInvisible("Article");
            gvHammadde.SetCaptions("HammaddeSira:#", "Iplik:" + Language.Sentez.Hammadde, "IplikAciklama:" + Language.Sentez.Aciklama, "IplikMiktar:" + Language.Sentez.Kullanim, "Detay:" + Language.Sentez.Detail);
            gvHammadde.BestFitColumns();

            gvAciklama.Init(ds.Tables[2]);
            gvAciklama.ViewCaption = Language.Sentez.StokKartiDetayi;
            gvAciklama.MakeColumnInvisible("Article");
            gvAciklama.SetCaptions("AciklamaSira:#", "Aciklama:" + Language.Sentez.Aciklama);
            gvAciklama.BestFitColumns();
        }

        private void cmbUretimTipi_SelectedIndexChanged(object sender, EventArgs e)
        {
            //switch (StokKartiTipi)
            //{
            //    case 1:
            //        UretimTipiChanged20();
            //        break;
            //    case 2:
            //        UretimTipiChanged11();
            //        break;
            //    case 3:
            //        break;
            //    case 4:
            //        break;
            //}
        }

        private void cmbCins_SelectedIndexChanged(object sender, EventArgs e)
        {
            //int duz = 0;
            //int bukle = 0;

            //if (txtKafa1.Text != "")
            //    if (cmbCins1.SelectedIndex == 0)
            //        duz++;
            //    else
            //        bukle++;

            //if (txtKafa2.Text != "")
            //    if (cmbCins2.SelectedIndex == 0)
            //        duz++;
            //    else
            //        bukle++;

            //if (txtKafa3.Text != "")
            //    if (cmbCins3.SelectedIndex == 0)
            //        duz++;
            //    else
            //        bukle++;

            //if (txtKafa4.Text != "")
            //    if (cmbCins4.SelectedIndex == 0)
            //        duz++;
            //    else
            //        bukle++;

            ////Düz Bukle
            ////Bukle Bukle
            ////Normal Düz
            ////1 Düz-2 Bukle
            ////2 Bukle-1 Düz
            ////2 Düz-2 Bukle
            ////3 Düz-1 Bukle

            //if (duz == 1 && bukle == 1)
            //    cmbCins.SelectedIndex = 0;
            //else if (duz == 0 && bukle == 2)
            //    cmbCins.SelectedIndex = 1;

            //else if (duz == 2 && bukle == 0)
            //    cmbCins.SelectedIndex = 2;

            //else if (duz == 1 && bukle == 2)
            //    cmbCins.SelectedIndex = 3;

            //else if (duz == 1 && bukle == 2)
            //    cmbCins.SelectedIndex = 4;

            //else if (duz == 2 && bukle == 2)
            //    cmbCins.SelectedIndex = 5;

            //else if (duz == 3 && bukle == 1)
            //    cmbCins.SelectedIndex = 6;
        }
        #endregion

        private void btnKaydet_Click(object sender, EventArgs e)
        {
            //gv1.EndEditing();
            //gv2.EndEditing();
            //gv3.EndEditing();
            //gv4.EndEditing();

            //AkerDB.ProductRow drProduct = Data.DB.Product.NewProductRow();
            //if (StokKartiTipi == 1)
            //    drProduct.Article = lblYeniKod20.Text;
            //else if (StokKartiTipi == 2)
            //{
            //    drProduct.Article = lblYeniKod11.Text;
            //    if (gleIp1.EditValue.ToStringNN() == "")
            //    {
            //        XtraMessageBox.Show("1.İp boş olamaz!");
            //        return;
            //    }
            //}


            //drProduct.ModifiedBy = AppSession.UserInfo.UserId;
            //drProduct.ModifiedOn = DateTime.Now;

            //Data.DB.Product.AddProductRow(drProduct);
            //Data.SaveProduct(drProduct);

            //AkerDB.ProductDetayRow drDetay = Data.DB.ProductDetay.NewProductDetayRow();
            //drDetay.ProductId = drProduct.ProductId;

            //if (StokKartiTipi == 1)
            //{
            //    Save20(drDetay);
            //    foreach (DataRow dr in (gvMaliyet20.DataSource as DataView).Table.Rows)
            //    {
            //        AkerDB.ProductMaliyetRow drMaliyet = Data.DB.ProductMaliyet.NewProductMaliyetRow();
            //        drMaliyet.ProductId = drProduct.ProductId;//BazProductId;
            //        drMaliyet.Aciklama = dr["MaliyetAciklama"].ToStringNN();
            //        drMaliyet.CurrencyId = dr["CurrencyId"].ToIntNN();
            //        drMaliyet.Fiyat = dr["Fiyat"].ToDecimalNN();
            //        Data.DB.ProductMaliyet.Rows.Add(drMaliyet);
            //        Data.TAM.ProductMaliyetTableAdapter.Update(drMaliyet);
            //    }
            //}
            //else if (StokKartiTipi == 2)
            //{
            //    Save11(drDetay);
            //    foreach (DataRow dr in (gvMaliyet11.DataSource as DataView).Table.Rows)
            //    {
            //        AkerDB.ProductMaliyetRow drMaliyet = Data.DB.ProductMaliyet.NewProductMaliyetRow();
            //        drMaliyet.ProductId = drProduct.ProductId;// BazProductId;
            //        drMaliyet.Aciklama = dr["MaliyetAciklama"].ToStringNN();
            //        drMaliyet.CurrencyId = dr["CurrencyId"].ToIntNN();
            //        drMaliyet.Fiyat = dr["Fiyat"].ToDecimalNN();
            //        Data.DB.ProductMaliyet.Rows.Add(drMaliyet);
            //        Data.TAM.ProductMaliyetTableAdapter.Update(drMaliyet);
            //    }
            //}




            //Data.DB.ProductDetay.Rows.Add(drDetay);
            //Data.TAM.ProductDetayTableAdapter.Update(drDetay);

            //btnKaydet.Enabled = false;

            //if (ProductInserted != null)
            //    ProductInserted(drProduct.ProductId, drProduct.Article, StokKartiTipi);

            //AppSession.MainForm.ShowMdiChildForm(typeof(ImalatKartlari), drProduct.ProductId);
        }

        private void gleMakineCinsi_EditValueChanged(object sender, EventArgs e)
        {
            //if (gleMakineCinsi.EditValue.ToIntNN() != -1)
            //{
            //    DataTable dt = Data.DB.OrmeMakinesi.Select("OrmeMakinesiId=" + gleMakineCinsi.EditValue).CopyToDataTable();
            //    txtPus.Text = dt.Rows[0]["PUS"].ToStringNN();
            //    txtFayn.Text = dt.Rows[0]["Fine"].ToStringNN();
            //    txtSistem.Text = dt.Rows[0]["Sistem"].ToStringNN();
            //}
        }

        private void gleBazArtikel20_EditValueChanged(object sender, EventArgs e)
        {
            //BazProductId = gleBazArtikel20.EditValue.ToIntNN();
            //Load20();
        } 
    }
}